package com.zmn.oms.services.impl.part;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.model.dto.work.modify.other.ZsPartReimburseDTO;
import com.zmn.oms.model.entity.part.OrderPart;
import com.zmn.oms.model.entity.part.OrderPartQuery;
import com.zmn.oms.persistence.interfaces.part.OrderPartDao;
import com.zmn.oms.services.interfaces.part.OrderPartService;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：订单配件
 * @author heciqi
 * @date 2018/11/28 14:44
 */
@Service
public class OrderPartServiceImpl implements OrderPartService {

    @Resource
    private OrderPartDao orderPartDao;

    /**
     * 按订单id查询
     *
     * @param orderId
     * @return
     */
    @Override
    public List<OrderPart> listByOrderId(Long orderId) {
        OrderPartQuery query = new OrderPartQuery();
        query.setOrderId(orderId);

        List<OrderPart> list = orderPartDao.listByQuery(query);
        for (OrderPart orderPart : list){
            // 内采配件，支付方式为0，则是老仓储数据，返回空List
            if (Objects.equals(orderPart.getPartType(), OrderConsts.PART_TYPE_SOURCING_IN)
                    && Objects.equals(orderPart.getPayType(), GlobalConsts.NONE)){
                return Lists.newArrayList();
            }
            // 外报和运杂配件，报销单号为null，则是老仓储数据，返回空List
            if ((Objects.equals(orderPart.getPartType(), OrderConsts.PART_TYPE_SOURCING_OUT)
                    || Objects.equals(orderPart.getPartType(), OrderConsts.PART_TYPE_DELIVER))
                    && Objects.isNull(orderPart.getReimburseId())){
                return Lists.newArrayList();
            }
        }
        return list;
    }

    @Override
    public List<OrderPart> listOrderPartByOrderId(Long orderId) {
        OrderPartQuery query = new OrderPartQuery();
        query.setOrderId(orderId);
        return orderPartDao.listByQuery(query);
    }

    /**
     * 按订单id查询
     *
     * @param orderId
     * @return
     */
    @Override
    public List<OrderPart> listByOrderIdSrcMaster(Long orderId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            OrderPartQuery query = new OrderPartQuery();
            query.setOrderId(orderId);

            return orderPartDao.listByQuery(query);
        }
    }

    /**
     * 按订单id查询
     * @param orderId
     * @param partType
     * @return
     */
    @Override
    public List<OrderPart> listByOrderId(Long orderId, Integer partType) {
        OrderPartQuery query = new OrderPartQuery();
        query.setOrderId(orderId);
        query.setPartType(partType);

        return orderPartDao.listByQuery(query);
    }

    /**
     * 根据类型查询配件（主库）
     *
     * @param orderId
     * @param partType
     * @return
     */
    @Override
    public List<OrderPart> listByOrderIdSrcMaster(Long orderId, Integer partType) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            OrderPartQuery query = new OrderPartQuery();
            query.setOrderId(orderId);
            query.setPartType(partType);

            return orderPartDao.listByQuery(query);
        }
    }

    /**
     * 按订单id和工程师id查询
     *
     * @param orderId
     * @param masterId
     * @return
     */
    @Override
    public List<OrderPart> listByOrderIdAndMasterId(Long orderId, Integer masterId) {
        OrderPartQuery query = new OrderPartQuery();
        query.setOrderId(orderId);
        query.setMasterId(masterId);

        return orderPartDao.listByQuery(query);
    }

    @Override
    public List<OrderPart> listEsOrderPartByOrderIdList(List<Long> orderIdList) {
        return orderPartDao.listEsOrderPartByOrderIdList(orderIdList);
    }

    /**
     * 根据订单id和报销id查询配件列表
     * @param orderId
     * @param reimburseId
     * @return java.util.List<com.zmn.oms.model.entity.part.OrderPart>
     * @author wangxiaokun
     */
    @Override
    public List<OrderPart> listByOrderIdAndReimburseId(Long orderId, String reimburseId) {
        return orderPartDao.listByOrderIdAndReimburseId(orderId, reimburseId);
    }

    /**
     * 根据订单id和报销id查询配件列表（主库）
     * @param orderId
     * @param reimburseId
     * @return java.util.List<com.zmn.oms.model.entity.part.OrderPart>
     * @author wangxiaokun
     */
    @Override
    public List<OrderPart> listByOrderIdAndReimburseIdSrcMaster(Long orderId, String reimburseId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderPartDao.listByOrderIdAndReimburseId(orderId, reimburseId);
        }
    }

    /**
     * 新增工单配件
     * @param orderParts
     * @return
     */
    @Override
    public Integer insertAll(List<OrderPart> orderParts) {
        orderParts.forEach(e -> {
            if (Objects.isNull(e.getPayType())) {
                e.setPayType(0);
            }
            if (Objects.isNull(e.getSettlementSubjectType())) {
                e.setSettlementSubjectType(0);
            }
        });
        return orderPartDao.insertAll(orderParts);
    }

    /**
     * 删除工单配件
     * @param orderId
     * @return
     */
    @Override
    public Integer deleteByOrderId(Long orderId) {
        return orderPartDao.deleteByOrderId(orderId);
    }

    /**
     * 删除配件
     * @param orderId
     * @return
     */
    @Override
    public Integer deleteByOrderIdAndPartType(Long orderId, Integer partType) {
        OrderPart orderPart = new OrderPart();
        orderPart.setOrderId(orderId);
        orderPart.setPartType(partType);
        return orderPartDao.deleteByOrderIdAndPartType(orderPart);
    }

    @Override
    public Integer deleteByOrderIdAndReimburseId(Long orderId, String reimburseId) {
        return orderPartDao.deleteByOrderIdAndReimburseId(orderId, reimburseId);
    }

    @Override
    public Integer updateOrderPartPriceByOrderId(ZsPartReimburseDTO zsPartReimburseDTO) {
        List<Integer> countList = zsPartReimburseDTO.getPartPricePostDTO().stream().map(
                partPrice ->{
                    OrderPart orderPart = new OrderPart();
                    orderPart.setOrderId(zsPartReimburseDTO.getOrderId());
                    orderPart.setOrderPartId(partPrice.getOrderPartId());
                    orderPart.setPurchasePrice(partPrice.getPrice());
                    orderPart.setDeclarePrice(partPrice.getDeclare());
                    orderPart.setCostPrice(partPrice.getPrice());
                    orderPart.setPrice(partPrice.getPrice());
                    return orderPartDao.updateByOrderPartByOrderPartId(orderPart);
                }).collect(Collectors.toList())
                .stream()
                .filter(amount -> amount.intValue() > 0)
                .distinct().collect(Collectors.toList());

        return countList.size();
    }

    @Override
    public Integer updateReimburseStatusByReimburseId(Long orderId, String reimburseId, Integer reimburseStatus){
        return orderPartDao.updateReimburseStatusByReimburseId(orderId, reimburseId, reimburseStatus);
    }

    @Override
    public Integer updateSettlementSubjectTypeByOrderPartId(OrderPart orderPart) {
        if (Objects.nonNull(orderPart)) {
            return orderPartDao.updateSettlementSubjectTypeByOrderPartId(orderPart);
        }
        return 0;
    }

    @Override
    public Integer updateAiStatusByOrderPartId(OrderPart orderPart) {
        if (Objects.nonNull(orderPart) && NumberUtil.isNotNullOrZero(orderPart.getOrderPartId())) {
            return orderPartDao.updateAiStatusByOrderPartId(orderPart);
        }
        return 0;
    }
}
